        var invoiceStore = new Ext.data.JsonStore({
    		url: '/weihe/getItems_invoiceList',
			root: 'datas',
			fields : [{
			        	   name:'invoiceId',mapping:'id'
			          },{
			        	   name:'invoiceAmount',mapping:'amount' 
			          },{
        				   name:'invoicePrice',mapping:'unitPrice'
        			  },{
        				   name:'invoiceUnit',mapping:'unit'
        			  },{
        				   name:'invoiceQty',mapping:'qty'
        			  },{
        				   name:'invoiceCount',mapping:'count'
        			  },{
        				   name:'invoiceDog',mapping:'dog'
        			  }],
					listeners :{ 
					   'beforeload':function(s,options) {
							var tag = 0;
							Ext.Ajax.request({
								url : '/weihe/getListJudge_invoiceList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									if (json.deny) {
										Ext.Msg.alert('错误',json.msg);
										tag = 1;
									}
										if(tag == 1){
											return false;
										}else{
											return true;						
										}
								},
								failure : function() {
									Ext.Msg.alert('错误', '失败!');
									tag =1;
								}
							});
			   }
			}
		});
        
        var invoiceListStore = new Ext.data.JsonStore({
			url: '/weihe/getList_invoiceList',
			root: 'datas',
			fields : [{
			        	   name:'invoiceListId',mapping:'id'
			          },{
			        	   name:'invoiceCompany',mapping:'company' 
			          },{
        				   name:'invoiceAddress',mapping:'address'
        			  },{
        				   name:'invoiceEmail',mapping:'email'
        			  },{
        				   name:'invoiceLinkman',mapping:'linkman'
        			  },{
        				   name:'invoiceContact',mapping:'contact'
        			  },{
        				   name:'invoicePayment',mapping:'payment'
        			  },{
        				   name:'invoiceDate', mapping:'date',type : 'date', dateFormat:"Y-m-d"
        			  },{
        				   name:'invoiceInvoiceNum',mapping:'invoiceNum'
        			  },{
        				   name:'invoiceTotal',mapping:'total'
        			  }],
					listeners :{ 
					   'beforeload':function(s,options) {
							var tag = 0;
							Ext.Ajax.request({
								url : '/weihe/getListJudge_invoiceList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									if (json.deny) {
										Ext.Msg.alert('错误',json.msg);
										tag = 1;
									}
										if(tag == 1){
											return false;
										}else{
											return true;						
										}
								},
								failure : function() {
									Ext.Msg.alert('错误', '失败!');
									tag =1;
								}
							});
			   }
			}
		});
        
       
        
invoices = function(){
	
  return{
	  
	  
		listPanel : function(){
	  
		var listPanel = new Ext.Panel( {
			                    region : 'north',
			                    layout : 'border',
			                    frame  : true,
			                    height :  250,
			                    items  : [invoices.listGrid(),invoices.listForm()] 
		                      });
	  
	  return listPanel;
},

listForm : function(){
	  
    


		var button_add = new Ext.Button({
			text : '添加',
			handler : function() {
				progressConsole();
				if(listForm.form.isValid()){ 
					Ext.getCmp("invoicesForm").buttons[0].enable();
					Ext.getCmp("invoicesForm").buttons[1].enable();
					Ext.getCmp("invoicesForm").buttons[2].enable();
					if (listForm.form.findField("invoiceListId").getValue() == "") {
						 listForm.form.submit({
						     url : '/weihe/add_invoiceList',
						     success : function(form, action) {
							      if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
								  }else{
								        Ext.Msg.alert('消息', action.result.msg, function() {
								        Ext.getCmp("invoiceListForm").form.findField('invoiceList.id').setValue(action.result.invoiceList_id);
									    Ext.getCmp("invoicesForm").form.findField('invoiceList.id').setValue(action.result.invoiceList_id);
									    Ext.getCmp("invoiceListGrid").getStore().load({params : {start : action.result.total-1, limit : 5}});
									    invoiceStore.removeAll();
									    listForm.form.reset();
								        });
							       }
						     }
					      });
					}else {
							listForm.form.submit({
								url : '/weihe/update_invoiceList',
								success : function(form, action) {
								  if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
								  }else{
										Ext.Msg.alert('消息', action.result.msg, function() {
											Ext.getCmp("invoiceListGrid").getStore().load({params : {start : 0, limit : 5}});
										});
									}
							   }
							});
					}
				}else{
					Ext.Msg.alert('提示','请输入相关内容');
				}
			}
		});

	var button_clear = new Ext.Button({
		text : '清空',
		handler : function() {
		
			listForm.form.reset();
			listForm.buttons[0].setText('添加');
			Ext.getCmp("invoicesForm").form.reset();
			Ext.getCmp("invoicesForm").buttons[0].disable();
		    Ext.getCmp("invoicesForm").buttons[1].disable();
		    Ext.getCmp("invoicesForm").buttons[2].disable();
		    Ext.getCmp("invoiceGrid").getStore().removeAll();
		}
	});

	var button_del = new Ext.Button({
		text : '删除',
		handler : function() {
			var id = listForm.form.findField('invoiceListId').getValue();
			if (id == '') {
				Ext.Msg.alert('提示', '请选择需要删除的信息!');
			} else {
				progressConsole();
				Ext.Ajax.request({
					url : '/weihe/delete_invoiceList',
					success : function(response) {
						var json = Ext.decode(response.responseText);
						if (json.success) {
						  if (json.deny) {
									Ext.Msg.alert('错误',json.msg);
									listForm.form.reset();
									listForm.buttons[0].setText('添加');
						  }else{
							Ext.Msg.alert('消息', json.msg, function() {
								Ext.getCmp("invoiceListGrid").getStore().load({params : {start : 0, limit : 5}});
								listForm.form.reset();
								listForm.buttons[0].setText('添加');
							});
						  }
						}else{
							Ext.Msg.alert('消息', json.msg);
						}
					},
					failure : function() {
						Ext.Msg.alert('错误', '删除失败!');
					},
					params: { 'invoiceList.id' : listForm.form.findField('invoiceListId').getValue() }
				});
			}
		}
	});
	
	 var paymentComboData = new Ext.data.SimpleStore({
		  fields : ['text'],
		  data : [['先款后货'],['先货后款'],['先订金 天后收余款']]
	  });
	
	  var companyStore = new Ext.data.JsonStore({
			url: '/weihe/getClients_invoiceList',
			root: 'names',
			fields: ['company']
		});
		
		var companyCombo = {
				id : 'invoiceCompany',
				xtype: 'combo',
				fieldLabel: '客户名称',
				store: companyStore,
				typeAhead: false,
				name: 'invoiceList.company',
				loadingText: '查找中...',
				emptyText: '请输入客户名称', 
				minChars: 1,
				pageSize: 0,
				hideTrigger: true,
				editable: true, 
				allowBlank:false,
				triggerAction: 'all',
				displayField : 'company',
				listeners :{ 'select':function(combo, record,index){
							Ext.Ajax.request({
								url : '/weihe/getClient_invoiceList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									  listForm.form.findField('invoiceList.address').setValue(json.address);
									  listForm.form.findField('invoiceList.email').setValue(json.email);
									  listForm.form.findField('invoiceList.linkman').setValue(json.linkman);
									  listForm.form.findField('invoiceList.contact').setValue(json.contact);
									},
								params : 'query=' + record.get('company')
							});
				    }
				}
		};
			var listForm = new Ext.form.FormPanel({
			id : 'invoiceListForm',
			region : 'east',
			title : '订单信息',
			frame : true,
			width : 300,
			autoScroll : true,
			labelAlign : 'right',
			labelWidth : 60,
			defaultType : 'textfield',
				defaults : {
				width : 200
			},
			items : [{
				id: 'invoiceListId',
				xtype : 'hidden',
				name : 'invoiceList.id'
			},{
				id: 'invoiceInvoiceNum',
				xtype : 'hidden',
				name : 'invoiceList.invoiceNum'
			},companyCombo,{
				id: 'invoiceAddress',
				fieldLabel: "地址",
				allowBlank:false,
				name: "invoiceList.address"
			},{
				id: 'invoiceEmail',
				fieldLabel: "EMAIL",
				allowBlank:false,
				name: "invoiceList.email"
			},{
				id: 'invoiceLinkman',
				fieldLabel: "联系人",
				allowBlank:false,
				name: "invoiceList.linkman"
			},{
				id: 'invoiceContact',
				fieldLabel: "联系方式",
				allowBlank:false,
				name: "invoiceList.contact"
			},{
				id: 'invoiceDate',
                xtype: 'datefield',
                fieldLabel: "订单日期",
                format : 'Y-m-d',
    			allowBlank:false,
                name: "invoiceList.date"
			},{
				id:'invoicePayment',
				fieldLabel : '支付方式',
 				name : 'invoiceList.payment',
 				xtype : 'combo',
 				store : paymentComboData,
 				emptyText : '请选择',
 				mode : 'local',
 				triggerAction : 'all',
 				allowBlank:false,
 				displayField : 'text'
			}],
			buttons : [button_add,button_clear,button_del]
	});
	  
	  return listForm;
},  

infoWin : function(){
  	  var infoWin = new Ext.Window({
  	  	id: 'coInfo',
        title:'订单查询',
        layout: 'fit',
        width:330,    
        height:250,
        modal: true,
        draggable : false,
        resizable: false,
        closeAction:"close",
        plain:true,    
        items:[invoices.queryForm()] 
        
    });  
  		return infoWin;
  	},
  	
  	queryForm : function(){
  		
  		 var paymentComboData = new Ext.data.SimpleStore({
		  fields : ['text'],
		  data : [['先款后货'],['先货后款'],['先订金']]
	  });
  		
  	 var queryForm = new Ext.form.FormPanel({
			baseCls:"x-plain",
			frame : true,
			autoHeight : true,
			labelAlign : 'right',
			labelWidth : 85,
			defaultType : 'textfield',
				defaults : {
				width : 200
			},
			items : [{
					  fieldLabel: "客户名称",
      	    		  name: "invoiceList.company"
			 	  },{
					  fieldLabel: "订单号",
      	    		  name: "invoiceList.invoiceNum"
			 	  },{
					  fieldLabel : '支付方式',
				      name : 'invoiceList.payment',
					  xtype : 'combo',
					  store : paymentComboData,
				      emptyText : '请选择',
					  mode : 'local',
					  triggerAction : 'all',
					  editable: false,
				      displayField : 'text'
				   },{
            		  xtype: 'datefield',
                      fieldLabel: "开始日期",
                      format : 'Y-m-d',
            		  name: "invoiceList.date"
				   },{
            		  xtype: 'datefield',
                      fieldLabel: "结束日期",
                      format : 'Y-m-d',
            		  name: "invoiceList.endDate"
				   }],
			buttons:[{
        		 text:'确认',
             	 handler:function(){
             	 	var company = queryForm.form.findField('invoiceList.company').getValue()
             	 	var payment = queryForm.form.findField('invoiceList.payment').getValue()
             	 	var invoiceNum = queryForm.form.findField('invoiceList.invoiceNum').getValue()
             	 	var date = queryForm.form.findField('invoiceList.date').getValue();
             	 	var endDate = queryForm.form.findField('invoiceList.endDate').getValue();
				 Ext.getCmp("invoiceListGrid").getStore().baseParams = {'flag': true,'invoiceList.company':company,
					 													  'invoiceList.payment':payment,
					 													  'invoiceList.invoiceNum':invoiceNum,
					 													  'invoiceList.date':date,
					 													  'invoiceList.endDate':endDate};
				 Ext.getCmp("invoiceListGrid").getStore().load({params : {start : 0, limit : 5}});
				 queryForm.form.reset();
                 Ext.getCmp("coInfo").close();
              	
              	}
              },{
                 text:'取消',
                 handler:function(){
                 	queryForm.form.reset();
                 	Ext.getCmp("coInfo").close();
                 }
              }]   
        });
        return queryForm;
  	
  	},
	  
	  
  listGrid : function(){
	  
		var pageToolBar = new Ext.PagingToolbar({
			pageSize : 5,
			store : invoiceListStore,
			displayInfo : true,
			displayMsg : '当前显示{0}-{1}条记录,共{2}条记录',
		    emptyMsg : "没有记录显示!",
		    firstText : '首页',
		    prevText : '上一页',
		    nextText : '下一页',
		    lastText : '尾页',
		    beforePageText : '第',
		    afterPageText : '页,共{0}页'
		});


		var rowSelectionModel = new Ext.grid.RowSelectionModel({singleSelect : true});
		
		var tmp='';
		var code='';
		var payment='';
		
	   var listGrid = new Ext.grid.GridPanel({
		   id:'invoiceListGrid',
		   tbar:[{
			  	text:"添加采购单",
				handler:function(){
			   				if(tmp!=''){
							Ext.getCmp('mainTab').openTab('orderShopping','订单_采购单');
							Ext.getCmp("osListForm").form.findField('shoppingList.code').setValue(code);
							Ext.getCmp("osListForm").form.findField('shoppingList.payment').setValue(payment);
							osListStore.baseParams = {'id' : tmp};
							osListStore.load({params : {start : 0, limit : 6}});
							inStore.load({params : {start : 0, limit : 6,'invoiceList.id' : tmp}});
							tmp='',code='',payment='';
							
			   				}else{
			   					Ext.Msg.alert('提示','请在下方表中选择需要添加采购单的订单！');
			   				}
						}
		   		},"-",{
	   						text:"输入查询条件",
								handler:function(){
										invoices.infoWin().show();
								}
	   					},"-",{
	   						text:"查看全部",
								handler:function(){
									Ext.getCmp("invoiceListGrid").getStore().baseParams = {'flag': false};
									Ext.getCmp("invoiceListGrid").getStore().load({params : {start : 0, limit : 5}});
								}
	   			},"-"],
		   region : 'center',
		   minColumnWidth : 35,
		   loadMask : {msg:'正在加载数据，请稍侯...'},
		   store : invoiceListStore,
		   columns: [
		             {header : '客户名称', dataIndex : 'invoiceCompany'},
		             {header : '地址', dataIndex : 'invoiceAddress'},
		             {header : 'EMAIl', dataIndex : 'invoiceEmail'},
		             {header : '联系人', dataIndex : 'invoiceLinkman'},
		             {header : '联系方式', dataIndex : 'invoiceContact'},
		             {header : '订单号', dataIndex : 'invoiceInvoiceNum'},
		             {header : '订单日期', dataIndex : 'invoiceDate',sortable : true,renderer:Ext.util.Format.dateRenderer('Y年m月d日')},
		             {header : '支付方式', dataIndex : 'invoicePayment'},
		             {header : '合计', dataIndex : 'invoiceTotal'}
		             ],
         sm : rowSelectionModel,
         listeners :{ 
			   'rowclick':function(grid, rowIndex, event) {
			   		var record = grid.getStore().getAt(rowIndex);
			   		tmp = record.id;
			   		code = record.get('invoiceInvoiceNum');
			   		payment = record.get('invoicePayment');
			   		Ext.getCmp("invoiceListForm").form.loadRecord(record);
			   		Ext.getCmp("invoicesForm").form.findField('invoiceList.id').setValue(record.id);
			   		Ext.getCmp("invoicesForm").buttons[0].enable();
					Ext.getCmp("invoicesForm").buttons[1].enable();
					Ext.getCmp("invoicesForm").buttons[2].enable();
					Ext.getCmp("invoiceListForm").buttons[0].setText('修改');
					Ext.getCmp("invoiceGrid").getStore().baseParams = {'invoiceList.id': record.id};
					Ext.getCmp("invoiceGrid").getStore().load({params : {start : 0, limit : 8}});
		   		}
		   },
		  bbar : pageToolBar
});
		return listGrid;
	},
  
    grid : function(){
	 


		var pageToolBar = new Ext.PagingToolbar({
			pageSize : 8,
			store : invoiceStore,
			displayInfo : true,
			displayMsg : '当前显示{0}-{1}条记录,共{2}条记录',
		    emptyMsg : "没有记录显示!",
		    firstText : '首页',
		    prevText : '上一页',
		    nextText : '下一页',
		    lastText : '尾页',
		    beforePageText : '第',
		    afterPageText : '页,共{0}页'
		   


		});


		var rowSelectionModel = new Ext.grid.RowSelectionModel({singleSelect : true});
		
		   
	   var grid = new Ext.grid.GridPanel({
		   id:'invoiceGrid',
		   title : '货物信息表',
		   region : 'center',
		   minColumnWidth : 100,
		   loadMask : {msg:'正在加载数据，请稍侯...'},
		   store : invoiceStore,
		   columns: [
		             {header : '货品名称', dataIndex : 'invoiceDog',width: 200},
		             {header : '数量', dataIndex : 'invoiceQty'},
		             {header : '采购数量', dataIndex : 'invoiceCount'},
		             {header : '单价', dataIndex : 'invoicePrice',width: 120},
		             {header : '总价', dataIndex : 'invoiceAmount',width: 140}
        ],
           sm : rowSelectionModel,
           listeners :{ 
			   'rowclick':function(grid, rowIndex, event) {
			   var record = grid.getStore().getAt(rowIndex);
			   Ext.getCmp("invoicesForm").form.loadRecord(record);
			   Ext.getCmp("invoicesForm").buttons[0].setText('修改');
			  
		   }
		},
		  viewConfig : {
		     forceFit : false
		  },
		  bbar : pageToolBar
});
		return grid;
	},
	invoicesForm : function(){
		
		var namestore = new Ext.data.JsonStore({
			url: '/weihe/getCargosByname_shoppingList',
			root: 'names',
			fields: ['name']
		});
		
		var nameCombo = {
				id : 'invoiceDog',
				xtype: 'combo',
				fieldLabel: '货物名称',
				store: namestore,
				typeAhead: false,
				name: 'invoiceItem.dog',
				loadingText: '查找中...',
				emptyText: '请输入产品名称', 
				minChars: 1,
				pageSize: 0,
				hideTrigger: true,
				editable: true,
				triggerAction: 'all',
				displayField : 'name',
				listeners :{ 'select':function(combo, record,index){
							Ext.Ajax.request({
								url : '/weihe/getName_depotList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									  invoicesForm.form.findField('invoiceUnit').setValue(json.unit);
									  invoicesForm.form.findField('invoicePrice').setValue(json.price);
									},
								params : 'query=' + record.get('name')
							});
				    }
				}
		}

		var button_add = new Ext.Button({
			text : '添加',
			disabled : true,
			handler : function() {

					progressConsole();
				if(invoicesForm.form.isValid()){ 	
					if (invoicesForm.form.findField("invoiceItem.id").getValue() == "") {
						invoicesForm.form.submit({
						  url : '/weihe/addItem_invoiceList',
						   success : function(form, action) {
							if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
							 }else{
								Ext.Msg.alert('消息', action.result.msg, function() {
									 var i = invoicesForm.form.findField('invoiceList.id').getValue();
									   invoicesForm.form.reset();
									   invoicesForm.form.findField('invoiceList.id').setValue(i);
									   Ext.getCmp("invoiceGrid").getStore().baseParams = {'invoiceList.id': i};
									   Ext.getCmp("invoiceGrid").getStore().load({params : {start : 0, limit : 8}});
									   Ext.getCmp("invoiceListGrid").getStore().reload();
									
								})
							  }
						    },
						    failure : function(form, action) {
      							Ext.Msg.alert('错误', action.result.msg);
      						}
					    })
					}else {
						invoicesForm.form.submit({
							  url : '/weihe/updateItem_invoiceList',
							   success : function(form, action) {
								 if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
								  }else{
									Ext.Msg.alert('消息', action.result.msg, function() {
										var i = invoicesForm.form.findField('invoiceList.id').getValue();
										invoicesForm.form.reset();
										 invoicesForm.form.findField('invoiceList.id').setValue(i);
										 Ext.getCmp("invoiceGrid").getStore().reload();
										 Ext.getCmp("invoiceListGrid").getStore().reload();
										 invoicesForm.buttons[0].setText('添加');
										
									})
								  }
							    },
							    failure : function(form, action) {
	      							Ext.Msg.alert('错误', action.result.msg);
	      						}
						    })

				
						
					}
			}else{
				Ext.Msg.alert('提示','请输入相关内容');
			}
				}
		
			
		});

	var button_clear = new Ext.Button({
		text : '清空',
		disabled : true,
		handler : function() {
		var i = invoicesForm.form.findField('invoiceList.id').getValue();
			invoicesForm.form.reset();
			invoicesForm.form.findField('invoiceList.id').setValue(i);
		    invoicesForm.buttons[0].setText('添加');
		}
	});

	var button_del = new Ext.Button({
		text : '删除',
		disabled : true,
		handler : function() {
			var id = invoicesForm.form.findField('invoiceItem.id').getValue();
			if (id == '') {
				Ext.Msg.alert('提示', '请选择需要删除的信息!');
			} else {
				progressConsole();
				Ext.Ajax.request({
					url : '/weihe/deleteItem_invoiceList',
					success : function(response) {
						var json = Ext.decode(response.responseText);
						if (json.success) {
						  if (json.deny) {
									Ext.Msg.alert('错误',json.msg);
									invoicesForm.form.reset();
									invoicesForm.buttons[0].setText('添加');
						   }else{
							Ext.Msg.alert('消息', json.msg, function() {
								Ext.getCmp("invoiceGrid").getStore().load({params : {start : 0, limit :8}});
								var i = invoicesForm.form.findField('invoiceList.id').getValue();
								invoicesForm.form.reset();
								invoicesForm.form.findField('invoiceList.id').setValue(i);
								invoicesForm.buttons[0].setText('添加');
								Ext.getCmp("invoiceGrid").getStore().reload();
								Ext.getCmp("invoiceListGrid").getStore().reload();
							});
						}
					   }
					},
					failure : function() {
						Ext.Msg.alert('错误', '删除失败!');
					},
					params: { 'invoiceItem.id': id , 'invoiceList.id' : invoicesForm.form.findField('invoiceList.id').getValue() }

				});
			}
		}
	});
	
		var invoicesForm = new Ext.form.FormPanel({
			id : 'invoicesForm',
			title : '货物信息',
			frame : true,
			width : 300,
			region : 'east',
			autoScroll : true,
			labelAlign : 'right',
			labelWidth : 60,
			defaultType : 'textfield',
				defaults : {
				width : 200
			},
			items : [{
				id: 'invoiceId',
				xtype : 'hidden',
				name : 'invoiceItem.id'
			},{
				xtype : 'hidden',
				name : 'invoiceList.id'
			},nameCombo,{
				id:'invoiceQty',
				fieldLabel : '数量',
				name : 'invoiceItem.qty',
				allowBlank:false,
				regex : /^[1-9]\d*$/,
			    regexText:'请输入有效的数量', 
				blankText:'请输入数量'
			},{
				id:'invoiceUnit',
				fieldLabel : '单位',
				allowBlank:false,
				name : 'invoiceItem.unit',
				blankText:'请输入单位'
			},{
				id:'invoicePrice',
				fieldLabel : '单价',
				allowBlank:false,
				name : 'invoiceItem.unitPrice',
				regex : /^\d+(\.\d+)?$/,
				regexText:'请输入有效的价格', 
				blankText:'请输入单价'
			}],
			buttons : [button_add,button_clear,button_del]
		
	});
		return invoicesForm;
	}
  }
}();








